%returns a test decision for the null hypothesis that the return of buy days, sell days and uncondtional days are random samples from normal distributions with equal means and variances
%input: buy_returns, sell_returns, unconditional_returns
%output: t_test_results
function [t_test_results t_test_p_values t_test_t_stat nBuy nSell mBuy mSell diffBS fractionB fractionS]= testMATTest(rBuy, rSell, rNormal)
    
    %restructure rBuy and rSell vectors
    rBuy = rBuy((isnan(rBuy)==0));
    rSell = rSell((isnan(rSell)==0));
    
    %calculate number of sell/buy signals
    nBuy = length(rBuy);
    nSell = length(rSell);

    %calculate means of buy/sell returns and the difference
    mBuy = mean(rBuy);
    mSell = mean(rSell);
    diffBS = mBuy - mSell;
          
    %t test the significance level of 5 percents
    [buy_unconditional_result buy_unconditional_p_value x buy_unconditional_stats] = ttest2(rBuy, rNormal, 0.05);
    [sell_unconditional_result sell_unconditional_p_value x sell_unconditional_stats] = ttest2(rSell, rNormal, 0.05);
    [buy_sell_result buy_sell_p_value x buy_sell_stats] = ttest2(rBuy, rSell, 0.05);
        
    t_test_p_values = [buy_unconditional_p_value sell_unconditional_p_value buy_sell_p_value];
    t_test_results = [buy_unconditional_result sell_unconditional_result buy_sell_result];
    t_test_statistics = [buy_unconditional_stats sell_unconditional_stats buy_sell_stats];
    t_test_t_stat = [t_test_statistics(1).tstat t_test_statistics(2).tstat t_test_statistics(3).tstat];
    
    %calculate the fraction of buy/sell returns which is greater than 0
    fractionB = length(rBuy((rBuy>0)))/(length(rBuy));
    fractionS = length(rSell((rSell>0)))/(length(rSell));

end




